Date.prototype.format = function(format){
    var o = {
        "M+" : this.getMonth()+1, //month
        "d+" : this.getDate(),    //day
        "h+" : this.getHours(),   //hour
        "m+" : this.getMinutes(), //minute
        "s+" : this.getSeconds(), //second
        "q+" : Math.floor((this.getMonth()+3)/3),  //quarter
        "S" : this.getMilliseconds() //millisecond
    }
	
    if(/(y+)/.test(format))
        format=format.replace(RegExp.$1,(this.getFullYear()+"").substr(4 - RegExp.$1.length));
		
    for(var k in o)
        if(new RegExp("("+ k +")").test(format))
            format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
	
    return format;
}

function fnIsIntNum(strNum, mixVal){
    var strCheckNum = strNum + "";
    if(strCheckNum.length < 1)
        return false;
    else if(isNaN(strCheckNum))
        return false;
    else if(parseInt(strCheckNum, 10) < mixVal)
        return false;
    else if(parseFloat(strCheckNum) > parseInt(strCheckNum, 10))
        return false;

    return true;
}

function fnComputerDay(intYear,intMonth){
    var dtmDate = new Date(intYear,intMonth,-1);
    var intDay = dtmDate.getDate() + 1;

    return intDay;
}	

/*add the validate year to 4 digits*/
function validateYear(strDate){
    var strCheckDate = strDate + "";
    var arrDate = strDate.split("/");
    if (arrDate[0].length != 2
        || arrDate[1].length != 2
        || arrDate[2].length != 4) {
        return false;
    } else if (!checkIsValidDate(strDate)) {
        return false;
    } else {
        return true;
    }
}

function checkIsValidDate(strDate){
    var strCheckDate = strDate;
	
    if(strCheckDate != ""){
        var arrDate;
        var regExpInfo = /\//;
        var intIndex = strCheckDate.search(regExpInfo);        
        if(intIndex == -1){
            return false;
        }else{
            arrDate = strCheckDate.split("/");
        }
	
        if(arrDate.length != 3){
            return false;
        }else{
            if(fnIsIntNum(arrDate[2], 1)){
                if(parseInt(arrDate[2], 10) < 1 || parseInt(arrDate[2], 10) > 9999){
                    return false;
                }
            }else{
                return false;
            }
            if(fnIsIntNum(arrDate[0], 1)){
                if(parseInt(arrDate[0], 10) < 1 || parseInt(arrDate[0], 10) > 12){
                    return false;
                }
            }else{
                return false;
            }
            if(fnIsIntNum(arrDate[1], 1)){
                var intDayCount = fnComputerDay(parseInt(arrDate[2], 10),parseInt(arrDate[0], 10));
                if(intDayCount < parseInt(arrDate[1], 10))
                {
                    return false;
                }
            }else{
                return false;
            }
        }
    }
    return true;
}

function checkDateEarlier(strStart,strEnd){
    var d1 = fixTimeFormat(strStart, '');
    var d2 = fixTimeFormat(strEnd, '');
    
    if(parseInt(d1, 10) > parseInt(d2, 10)){         
        return false;
    }else{        
        return true;
    }
}

function checkIsValidTime(strTime){
    var strCheckTime = strTime + "";
	
    if(strCheckTime == ""){
        return false;
    }
	
    var arrTime;
    var regExpInfo = /:/;
    var intIndex = strCheckTime.search(regExpInfo);
    if(intIndex == -1){
        return false;
    }else{
        arrTime = strCheckTime.split(":");
    }

    if(arrTime.length != 3){
        return false;
    }else{
        if(fnIsIntNum(arrTime[0], 0)){
            if(parseInt(arrTime[0], 10) < 0 || parseInt(arrTime[0], 10) > 23){
                return false;
            }
        }else{
            return false;
        }
		
        if(fnIsIntNum(arrTime[1], 0)){
            if(parseInt(arrTime[1], 10) < 0 || parseInt(arrTime[1], 10) > 59){
                return false;
            }
        }else{
            return false;
        }
		
        if(fnIsIntNum(arrTime[2], 0)){
            if(parseInt(arrTime[2], 10) < 0 || parseInt(arrTime[2], 10) > 59){
                return false;
            }
        }else{
            return false;
        }
    }
	
    return true;
}

function checkTimeEarlier(strStart,strEnd){
    if (( strStart == "" ) || ( strEnd == "" ))
        return true;
		
    if(checkIsValidTime(strStart) == false || checkIsValidTime(strEnd) == false)
        return false;
		
    var d1 = fixTimeFormat(strStart, '');
    var d2 = fixTimeFormat(strEnd, '');

    if(parseInt(d1, 10) > parseInt(d2, 10))
        return false;
    else
        return true;
}

function checkDateTimeEarlier(strStart,strEnd){
    if (( strStart == "" ) || ( strEnd == "" ))
        return true;

    var arrStart = strStart.split(" ");
    var arrEnd = strEnd.split(" ");
	
    if(checkIsValidDate(arrStart[0]) == false || checkIsValidDate(arrEnd[0]) == false)
        return false;
		
    if(checkIsValidTime(arrStart[1]) == false || checkIsValidTime(arrEnd[1]) == false)
        return false;
		
    var arrDate1 = fixTimeFormat(arrStart[0], '');
    var arrDate2 = fixTimeFormat(arrEnd[0], '');
    var arrTime1 = fixTimeFormat(arrStart[1], '');
    var arrTime2 = fixTimeFormat(arrEnd[1], '');
	
    var d1 = arrDate1 + arrTime1;
    var d2 = arrDate2 + arrTime2;

    if(parseInt(d1,10) > parseInt(d2,10))
        return false;
    else
        return true;
}

function usDate2AsiaDate(strDate){
    if(checkIsValidDate(strDate) == false){
        return strDate;
    }
	
    var arrDate = strDate.split("/");
	
    return arrDate[2] + '/' + arrDate[0] + '/' + arrDate[1];
}

function asiaDate2UsDate(strDate){
    //	if(checkIsValidDate(strDate) == false){
    //		return strDate;
    //	}
	
    var arrDate = strDate.split("/");
	
    return arrDate[1] + '/' + arrDate[2] + '/' + arrDate[0];
}

/*
	Example:
		method									return
		fixTimeFormat('0:1:2', '')			000102
		fixTimeFormat('0:1:2', ':')		00:01:02
		fixTimeFormat('3-8-2007', '')		20070308
		fixTimeFormat('3-8-2007', '-')	03-31-2007
*/
function fixTimeFormat(strTime,splitMark){
    var arr1, isDate, result;
    var intIndex = strTime.search(/\//);
	
    if(intIndex == -1){
        intIndex = strTime.search(/:/);
        if(intIndex == -1){
            return strTime;
        }else{
            isDate = false;
            arr1 = strTime.split(":");
        }
    }else{
        isDate = true;
        arr1 = strTime.split('/');
    }

    for(var i=0; i<3; i++){
        arr1[i] = arr1[i].replace(/^0+/, "");
    }
	
    for (var i=0; i<3; i++){
        if(arr1[i].length == 0)
            arr1[i] = '0';
    }
    if(arr1[0].length == 1)
        arr1[0] = "0" + arr1[0];
    if(arr1[1].length == 1)
        arr1[1] = "0" + arr1[1];
    if(arr1[2].length == 1)
        arr1[2] = "0" + arr1[2];
		
    if(isDate && splitMark == '')//to iso date format, in favour of compare
        result = arr1[2] + splitMark + arr1[0] + splitMark + arr1[1];
    else
        result = arr1[0] + splitMark + arr1[1] + splitMark + arr1[2];
	
    return result;
}